<template>
  <div class="mybatis">
    <el-input class="left-input" type="textarea" :rows="25" v-model="input"></el-input>
    <ul class="button-list">
      <li>
        <el-button type="text" @click="selectClick">生成select</el-button>
      </li>
      <li>
        <el-button type="text" @click="selectPageClick">生成selectPage</el-button>
      </li>
      <li>
        <el-button type="text" @click="selectSearchClick">生成selectSearch</el-button>
      </li>
      <li>
        <el-button type="text" @click="insertClick">生成insert</el-button>
      </li>
      <li>
        <el-button type="text" @click="updateClick">生成update</el-button>
      </li>
    </ul>
    <el-input class="right-input" type="textarea" :rows="25" v-model="output"></el-input>
  </div>
</template>

<script type="text/ecmascript-6">
  import select from './select'
  import insert from './insert'
  import update from './update'
  /**
   * @author 白雨浓
   * @date 2017/12/23 12:05
   *
   * mybatis
   **/
  const TYPES = [
    'String', 'Date',
    'int', 'Integer',
    'boolean', 'Boolean',
    'double', 'Double',
    'float', 'Float',
  ];
  export default{
    name: 'AcMybatis',
    data(){
      return {
        input: '', /*左侧输入*/
        output: '', /*右侧输出*/
        tableName: '',
        params: [],
      }
    },
    mounted(){
      this.$nextTick(() => {
      })
    },
    methods: {
      handleParams(){
        this.params = [];
        const lines = this.input.split('\n');
        for (const line of lines) {
          if (line.indexOf('private') >= 0) {
            let ps = line.substring(0, line.indexOf(';')).split(' ');
            ps = ps.filter(p => p !== '' && p !== ' ');
            if (ps.length !== 3) continue;
            if (TYPES.indexOf(ps[1]) < 0) continue;
            this.params.push({type: ps[1], name: ps[2]});
          } else if (line.indexOf('class') >= 0) {
            let cs = line.split(' ');
            cs = cs.filter(c => c !== '' && c !== ' ');
            this.tableName = this.toUnderline(cs[cs.indexOf('class') + 1])
              .replace('_entity', '').replace('_bean', '');
          }
        }
      },
      selectClick(){
        this.handleParams();
        select.select(this);
      },
      selectPageClick(){
        this.handleParams();
        select.selectPage(this);
      },
      selectSearchClick(){
        this.handleParams();
        this.$prompt('输入keyword', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({value}) => {
          select.selectSearch(this, value);
        });
      },
      insertClick(){
        this.handleParams();
        insert.insert(this);
      },
      updateClick(){
        this.handleParams();
        update.update(this);
      },
      /** 转下划线 */
      toUnderline(s){
        s = s.replace(/([A-Z])/g, "_$1").toLowerCase();
        if (s[0] === '_') s = s.substring(1);
        return s;
      },
      /** 转驼峰 */
      toCamel(s){
        return s.replace(/_(\w)/g, (all, letter) => letter.toUpperCase());
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  .mybatis {
    display flex
    justify-content space-around
    width 90%
    margin 50px auto

    .left-input {
      width 45%
      height 500px
    }

    .right-input {
      width 45%
      height 500px
    }

    .button-list {
      list-style none
      margin-top 200px
    }
  }
</style>
